/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.activity;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.handler.FastestRouteSearchBookmarkMenuHandler;
import lt.marsrutai.mobile.android.fragment.FastestRoutesSearchFragment;
import lt.marsrutai.mobile.android.fragment.NameDialogFragment.NameDialogHandler;
import lt.marsrutai.mobile.android.fragment.PointSelectionDialogFragment;
import lt.marsrutai.mobile.android.handler.menu.MenuHandler;
import lt.marsrutai.mobile.android.model.FastestRouteSearchBookmark;
import lt.marsrutai.mobile.android.service.BookmarkService;
import lt.marsrutai.mobile.android.util.BookmarkControlManager.BookmarkInfoProvider;
import lt.marsrutai.mobile.android.widget.DialogChoiceItem;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.support.v4.app.Fragment;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;

public class FastestRoutesSearchActivity extends BaseActivity implements PointSelectionDialogFragment.PointSelectionDialogListener,
        OnDateSetListener, OnTimeSetListener, NameDialogHandler, BookmarkInfoProvider<FastestRouteSearchBookmark> {

    @Override
    protected void setupActionBar(final ActionBar actionBar) {
        super.setupActionBar(actionBar);

        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(getString(R.string.fastest_routes));
    }

    @Override
    public MenuHandler getMenuHandler() {
        return new FastestRouteSearchBookmarkMenuHandler(this);
    }

    @Override
    public int getMenuResource() {
        return R.menu.fastest_route_search_menu;
    }

    @Override
    protected int getNavigationMode() {
        return ActionBar.NAVIGATION_MODE_STANDARD;
    }

    @Override
    public void setupView() {
        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            Fragment fragment = new FastestRoutesSearchFragment();
            fragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, fragment).commit();
        }
    }

    @Override
    public void onDialogClick(String ownerId, DialogChoiceItem item) {
        FastestRoutesSearchFragment fragment = (FastestRoutesSearchFragment) getSupportFragmentManager().findFragmentById(
                android.R.id.content);

        if (fragment != null) {
            fragment.dialogItemSelected(ownerId, item);
        }
    }

    @Override
    public boolean isSelected(String ownerId, DialogChoiceItem item) {
        FastestRoutesSearchFragment fragment = (FastestRoutesSearchFragment) getSupportFragmentManager().findFragmentById(
                android.R.id.content);

        if (fragment != null) {
            return fragment.isSelected(ownerId, item);
        }
        return false;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        FastestRoutesSearchFragment fragment = (FastestRoutesSearchFragment) getSupportFragmentManager().findFragmentById(
                android.R.id.content);

        if (fragment != null) {
            fragment.onDateSet(view, year, monthOfYear, dayOfMonth);
        }
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        FastestRoutesSearchFragment fragment = (FastestRoutesSearchFragment) getSupportFragmentManager().findFragmentById(
                android.R.id.content);

        if (fragment != null) {
            fragment.onTimeSet(view, hourOfDay, minute);
        }
    }

    @Override
    public FastestRouteSearchBookmark getBookmark() {
        FastestRoutesSearchFragment fragment = (FastestRoutesSearchFragment) getSupportFragmentManager().findFragmentById(
                android.R.id.content);

        return fragment.getBookmarkRepresentation();
    }

    @Override
    public void onDialogConfirm(String name) {
        FastestRouteSearchBookmark bookmark = getBookmark();
        bookmark.setName(name);
        int result = AppContext.getBookmarkService().updateOrAddBookmark(bookmark);
        int message = 0;

        switch (result) {
        case BookmarkService.BOOKMARK_ADDED:
            message = R.string.bookmark_added;
            break;
        case BookmarkService.BOOKMARK_NOT_ADDED:
            message = R.string.bookmark_not_added;
            break;
        case BookmarkService.BOOKMARK_UPDATED:
            message = R.string.bookmark_updated;
            break;
        case BookmarkService.BOOKMARK_NOT_UPDATED:
            message = R.string.bookmark_not_updated;
            break;
        }

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

}
